﻿Public Class ActBenefice
    Dim db As New Connexion
    Public idActiv As Integer
    Dim Entrer, action As String
    Public databaseName As String
    Private Sub ActBenefice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Entrer = ""
        DataGridViewBenf.MultiSelect = False
        DataGridViewBenf.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewBenf.ReadOnly = True
        majType()
        majPopulation()

        miseajour()
    End Sub

    Private Sub miseajour()
        Try
            db.myReq = "SELECT * FROM  [dbo].[ACTIVBENEFSIT2]"
            db.Connecter(databaseName)
            DataGridViewBenf.Rows.Clear()
            While db.rdr.Read()
                DataGridViewBenf.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4), db.rdr(5), db.rdr(6), db.rdr(7))
            End While
            db.Disconnecter()

            If DataGridViewBenf.RowCount - 1 = 0 Then
                Me.TextBoxEch.Text = ""
                Me.TextBoxRend.Text = ""
            Else
                DataGridViewBenf.Rows(0).Selected = True
                Me.TextBoxEch.Text = DataGridViewBenf.SelectedCells(5).Value
                Me.TextBoxRend.Text = DataGridViewBenf.SelectedCells(8).Value
                Me.DateTimePickerD.Text = DataGridViewBenf.SelectedCells(7).Value
                Me.DateTimePickerF.Text = DataGridViewBenf.SelectedCells(6).Value
            End If

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try

    End Sub

    Private Sub majType()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[BenfTyp]"
            db.Connecter(databaseName)
            Me.ComboBoxIdType.Items.Clear()
            Me.ComboBoxType.Items.Clear()
            Me.ComboBoxIdType.Items.Add("")
            Me.ComboBoxType.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxIdType.Items.Add(db.rdr(0))
                Me.ComboBoxType.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxIdType.SelectedIndex = 0
            Me.ComboBoxType.SelectedIndex = 0
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub ComboBoxType_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxType.TextChanged

        If Me.ComboBoxType.FindString(Me.ComboBoxType.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxType.Text.Length
            Me.ComboBoxType.SelectedIndex = Me.ComboBoxType.FindString(Me.ComboBoxType.Text)
            Me.ComboBoxType.SelectionStart = Pos
            Me.ComboBoxType.SelectionLength = Me.ComboBoxType.Text.Length - Pos
            Me.ComboBoxIdType.SelectedIndex = Me.ComboBoxType.FindString(Me.ComboBoxType.Text)
        Else
            Me.ComboBoxIdType.Text = ""
        End If

    End Sub

    Private Sub majPopulation()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[LocaliteSite]"
            db.Connecter(databaseName)
            Me.ComboBoxIdPop.Items.Clear()
            Me.ComboBoxPopulation.Items.Clear()
            Me.ComboBoxIdPop.Items.Add("")
            Me.ComboBoxPopulation.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxIdPop.Items.Add(db.rdr(0))
                Me.ComboBoxPopulation.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxIdPop.SelectedIndex = 0
            Me.ComboBoxPopulation.SelectedIndex = 0
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxPopulation_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxPopulation.TextChanged

        If Me.ComboBoxPopulation.FindString(Me.ComboBoxPopulation.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxPopulation.Text.Length
            Me.ComboBoxPopulation.SelectedIndex = Me.ComboBoxPopulation.FindString(Me.ComboBoxPopulation.Text)
            Me.ComboBoxPopulation.SelectionStart = Pos
            Me.ComboBoxPopulation.SelectionLength = Me.ComboBoxPopulation.Text.Length - Pos
            Me.ComboBoxIdPop.SelectedIndex = Me.ComboBoxPopulation.FindString(Me.ComboBoxPopulation.Text)
        Else
            Me.ComboBoxIdPop.Text = ""
        End If

    End Sub

    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        Me.TextBoxEch.Text = ""
        Me.TextBoxRend.Text = ""
        action = "ajouter"
    End Sub

    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Try
            Entrer = ""

            If action = "ajouter" Then
                db.myReq = "INSERT INTO  [dbo].[ACTIVBENEFSIT2] ([ID_TypBenf],[Id_Activ],[Id_Pop],[RVNBNF_BNFACT],[ECHTN_BNFACT],[DATED_BNFACT],[DATEF_BNFACT]) VALUES('" & Me.ComboBoxIdType.Text & "','" & idActiv & "','" & Me.ComboBoxIdPop.Text & "','" & Me.TextBoxRend.Text & "','" & Me.TextBoxEch.Text & "','" & Me.DateTimePickerD.Text & "','" & Me.DateTimePickerF.Text & "')"

            Else
                db.myReq = "PDATE  [dbo].[ACTIVBENEFSIT2] SET [ID_TypBenf] = '" & Me.ComboBoxIdType.Text & "',[Id_Activ]= '" & idActiv & "',[Id_Pop] = '" & Me.ComboBoxIdPop.Text & "',[RVNBNF_BNFACT] = '" & Me.TextBoxRend.Text & "',[ECHTN_BNFACT] = '" & Me.TextBoxEch.Text & "',[DATED_BNFACT] = '" & Me.DateTimePickerD.Text & "',[DATEF_BNFACT] = '" & Me.DateTimePickerF.Text & "' WHERE [ID_TypBenf]= '" & Me.DataGridViewBenf.SelectedCells(0).Value & "'"
            End If
            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        miseajour()

    End Sub
    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Try
            db.myReq = " DELETE FROM  [dbo].[ACTIVHUMSIT] WHERE [ID_TypBenf]= '" & Me.DataGridViewBenf.SelectedCells(0).Value & "'"
            db.Connecter(databaseName)
            db.Disconnecter()
            miseajour()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
End Class